package com.yskj.dngame.kotiledemo

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.google.gson.Gson
import com.yskj.dngame.kotiledemo.adapter.DataAdapter
import com.yskj.dngame.kotiledemo.bean.DataAdapterBean
import com.yskj.dngame.kotiledemo.util.LogUtil
import com.yskj.dngame.kotiledemo.view.DataView
import kotlinx.android.synthetic.main.activity_main.*

/**
 * Created by SYZ
on 2017/8/28.
 */
class MainActivity : Activity(), DataView {


    val context: Context? = null
    val listData: MutableList<DataAdapterBean> = ArrayList<DataAdapterBean>()
    val dataAdapter: DataAdapter by lazy { DataAdapter(this, listData) }

    val nameArray: Array<String> = arrayOf("王小明", "二麻子", "张三", "李四", "孙二狗")

    var datas: String = "1314"

    /**
     * 启动
     * 打开默认的生命周期 oncreate
     */
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    /**
     * 初始化
     */
    private fun initView() {
        //设置recycle 展示样式
        recycler.layoutManager = LinearLayoutManager(this)
        //设置recycle 的适配器
        recycler.adapter = dataAdapter
        //回调 ,监听事件
        dataAdapter.setOnItem(this)
        //回调 ,监听事件  单个监听
//        dataAdapter.setOnItem(object :DataView{
//            override fun onClick(position: Int) {
//                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
//            }
//
//            override fun onDatas(names: String?) {
//                TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
//            }
//
//        })
        //创建一组虚拟数据 ,更新到适配器中
        for (item in nameArray.indices) {
            var names: String = "${nameArray[item]}"
            var dataBean: DataAdapterBean = DataAdapterBean(names, item)
            listData.add(dataBean)
            LogUtil.i("_____" + names)
        }
        LogUtil.i(Gson().toJson(listData))
        //更新适配器数据
        dataAdapter.setData(listData)


    }

    /**
     * 单个选择item
     */
    override fun onClick(position: Int) {
        LogUtil.i("点击了下标:" + position)
        isData(position)
        setWhen1(position)
    }

    /**
     * 单个点击item 的名字
     */
    override fun onDatas(names: String?) {
        LogUtil.i("点击了item:" + names)
//        if (names != null) {
//            isData(names)
//        }
        names?.let { isData(it) }
//        isData(names!!)
    }

    /**
     * 使用 is  做参数判断
     */
    fun isData(nums: Any) {
        if (nums is String) {//如果为字符串类型时
            LogUtil.i("String 类型:" + nums)
        } else if (nums is Int) {
            LogUtil.i("Int 类型:" + nums)
        }
    }

    /**
     * 尝试 when
     * when表达式有参数：实现switch()语句效果
     */
    fun setWhen1(nums: Int) {
        when (nums) {
            1 -> LogUtil.d("点击了Item" + nums)

            2 -> LogUtil.d("点击了Item ****2****")

        }

    }


}



